﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Directory.DAL
{
	public class DBQuery:Directory.IDAL.IDBQuery
	{
		private static string sqlconnectionstring = ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString;


		#region IDBQuery 成员

		int Directory.IDAL.IDBQuery.ExecuteNonQuery(string query)
		{
			SqlConnection conn = new SqlConnection(sqlconnectionstring);
			SqlCommand cmd = new SqlCommand(query, conn);
			int rowcounts;

			conn.Open();
			try
			{
				rowcounts= cmd.ExecuteNonQuery();
			}
			finally
			{
				conn.Close();
			}
			return rowcounts;
		}

		object Directory.IDAL.IDBQuery.ExecuteScalar(string query)
		{
			SqlConnection conn = new SqlConnection(sqlconnectionstring);
			SqlCommand cmd = new SqlCommand(query, conn);

			object obj=null;

			conn.Open();
			try
			{
				obj = cmd.ExecuteScalar();
			}
			finally
			{
				conn.Close();
			}
			return obj;
		}

		DataSet Directory.IDAL.IDBQuery.ExecuteReader(string query)
		{
			SqlConnection conn = new SqlConnection(sqlconnectionstring);
			DataSet ds = new DataSet();
			SqlCommand cmd = new SqlCommand(query, conn);

			conn.Open();
			try
			{
				using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
				{
					sda.Fill(ds);
				}
			}
			finally
			{
				conn.Close();
			}
			return ds;
		}

		#endregion
	}
}
